﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WangMiEntity.EmployeeService;
using WangMiBusiness.GridViewModel;
using WangMiEntity.GridViewModel;
using WangMiEntity.Order;
using WangMiBusiness.BusinessConstant;

namespace WangMiBusiness.EmployeeService
{
    public class XiaoShouLiangBusiness
    {
        /// <summary>
        /// 按日期排列
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public List<XiaoShouLiangDateEntity> GetXiaoShouLiangByDate(DateTime startTime, DateTime endTime, string session, bool isContainALLDate)
        {
            List<XiaoShouLiangDateEntity> xiaoShouLiangDateEntityList = new List<XiaoShouLiangDateEntity>();
            var gridViewModel = new GridViewModelBusiness().GetBaseGridViewModel(startTime, endTime, session);

            gridViewModel.ForEach(item =>
            {
                XiaoShouLiangDateEntity xiaoShouLiangDateEntity = new XiaoShouLiangDateEntity();
                xiaoShouLiangDateEntity.OrderDate = item.OrderDate;
                xiaoShouLiangDateEntity.TotalOrders = GetTotalOrders(item);
                xiaoShouLiangDateEntity.UnpaidOrders = GetUnpaidOrders(item);
                xiaoShouLiangDateEntity.SilentOrders = GetPaidOrders(item);
                xiaoShouLiangDateEntity.TeamOrders = GetPaidOrders(item);
                xiaoShouLiangDateEntity.RefundingOrders = GetRefundingOrders(item);
                xiaoShouLiangDateEntity.RefundOrders = GetRefundOrders(item);

                xiaoShouLiangDateEntityList.Add(xiaoShouLiangDateEntity);
            });

            return xiaoShouLiangDateEntityList;
        }

        /// <summary>
        /// 获取总订单数
        /// </summary>
        /// <param name="baseGridModel"></param>
        protected int GetTotalOrders(BaseGridModel baseGridModel)
        {
            int count = 0;
            count += baseGridModel.SellerConsignedPartModel.Count;
            count += baseGridModel.TradeBuyerSignedModel.Count;
            count += baseGridModel.TradeClosedByTaoBaoModel.Count;
            count += baseGridModel.TradeClosedModel.Count;
            count += baseGridModel.TradeFinishedModel.Count;
            count += baseGridModel.TradeNoCreatePayModel.Count;
            count += baseGridModel.WaitBuyerConfirmGoodsModel.Count;
            count += baseGridModel.WaitBuyerPayModel.Count;
            count += baseGridModel.WaitSellerSendGoodsModel.Count;

            return count;
        }

        /// <summary>
        /// 获取未付款订单数
        /// </summary>
        /// <param name="baseGridModel"></param>
        protected int GetUnpaidOrders(BaseGridModel baseGridModel)
        {
            return baseGridModel.WaitBuyerPayModel.Count(item => item.Trade.Status == OrderStatus.WAIT_BUYER_PAY);
        }

        /// <summary>
        /// 获取已付款订单数
        /// </summary>
        /// <param name="baseGridModel"></param>
        /// <returns></returns>
        public double GetPaidOrders(BaseGridModel baseGridModel)
        {
            double count = 0;

            count += baseGridModel.WaitSellerSendGoodsModel.Count(item =>
            {
                return item.Trade.Orders.Any(p => p.RefundStatus == RefundStatus.NO_REFUND);
            });
            count += baseGridModel.SellerConsignedPartModel.Count(item =>
            {
                return item.Trade.Orders.Any(p => p.RefundStatus == RefundStatus.NO_REFUND);
            });
            count += baseGridModel.WaitBuyerConfirmGoodsModel.Count(item =>
            {
                return item.Trade.Orders.Any(p => p.RefundStatus == RefundStatus.NO_REFUND);
            });
            count += baseGridModel.TradeBuyerSignedModel.Count(item =>
            {
                return item.Trade.Orders.Any(p => p.RefundStatus == RefundStatus.NO_REFUND);
            });
            count += baseGridModel.TradeFinishedModel.Count(item =>
            {
                return item.Trade.Orders.Any(p => p.RefundStatus == RefundStatus.NO_REFUND);
            });

            return count;
        }

        /// <summary>
        /// 获取退款中订单数
        /// </summary>
        /// <param name="baseGridModel"></param>
        /// <returns></returns>
        public double GetRefundingOrders(BaseGridModel baseGridModel)
        {
            double count = 0;

            count += baseGridModel.SellerConsignedPartModel.Count(item =>
            {
                return item.Trade.Orders.Any(n => n.RefundStatus == RefundStatus.WAIT_SELLER_AGREE || n.RefundStatus == RefundStatus.WAIT_BUYER_RETURN_GOODS || n.RefundStatus == RefundStatus.WAIT_SELLER_CONFIRM_GOODS || n.RefundStatus == RefundStatus.SELLER_REFUSE_BUYER);
            });

            count += baseGridModel.TradeBuyerSignedModel.Count(item =>
            {
                return item.Trade.Orders.Any(n => n.RefundStatus == RefundStatus.WAIT_SELLER_AGREE || n.RefundStatus == RefundStatus.WAIT_BUYER_RETURN_GOODS || n.RefundStatus == RefundStatus.WAIT_SELLER_CONFIRM_GOODS || n.RefundStatus == RefundStatus.SELLER_REFUSE_BUYER);
            });

            count += baseGridModel.TradeFinishedModel.Count(item =>
            {
                return item.Trade.Orders.Any(n => n.RefundStatus == RefundStatus.WAIT_SELLER_AGREE || n.RefundStatus == RefundStatus.WAIT_BUYER_RETURN_GOODS || n.RefundStatus == RefundStatus.WAIT_SELLER_CONFIRM_GOODS || n.RefundStatus == RefundStatus.SELLER_REFUSE_BUYER);
            });

            count += baseGridModel.WaitBuyerConfirmGoodsModel.Count(item =>
            {
                return item.Trade.Orders.Any(n => n.RefundStatus == RefundStatus.WAIT_SELLER_AGREE || n.RefundStatus == RefundStatus.WAIT_BUYER_RETURN_GOODS || n.RefundStatus == RefundStatus.WAIT_SELLER_CONFIRM_GOODS || n.RefundStatus == RefundStatus.SELLER_REFUSE_BUYER);
            });

            count += baseGridModel.WaitSellerSendGoodsModel.Count(item =>
            {
                return item.Trade.Orders.Any(n => n.RefundStatus == RefundStatus.WAIT_SELLER_AGREE || n.RefundStatus == RefundStatus.WAIT_BUYER_RETURN_GOODS || n.RefundStatus == RefundStatus.WAIT_SELLER_CONFIRM_GOODS || n.RefundStatus == RefundStatus.SELLER_REFUSE_BUYER);
            });

            return count;
        }

        /// <summary>
        /// 获取已退款订单数
        /// </summary>
        /// <param name="baseGridModel"></param>
        /// <returns></returns>
        public double GetRefundOrders(BaseGridModel baseGridModel)
        {
            double count = 0;

            count += baseGridModel.SellerConsignedPartModel.Count(item =>
            {
                return item.Trade.Orders.Any(p => p.RefundStatus == RefundStatus.SUCCESS);
            });

            count += baseGridModel.TradeBuyerSignedModel.Count(item =>
            {
                return item.Trade.Orders.Any(p => p.RefundStatus == RefundStatus.SUCCESS);
            });

            count += baseGridModel.TradeClosedModel.Count(item =>
            {
                return item.Trade.Orders.Any(p => p.RefundStatus == RefundStatus.SUCCESS);
            });

            count += baseGridModel.WaitBuyerConfirmGoodsModel.Count(item =>
            {
                return item.Trade.Orders.Any(p => p.RefundStatus == RefundStatus.SUCCESS);
            });

            count += baseGridModel.WaitSellerSendGoodsModel.Count(item =>
            {
                return item.Trade.Orders.Any(p => p.RefundStatus == RefundStatus.SUCCESS);
            });

            return count;
        }
    }
}
